package com.liushuai.gateway.customize;

import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.handler.AsyncPredicate;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.server.ServerWebExchange;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * @author ：liushuai
 * @date ：Created in 2022/8/5 10:53
 * @description：自定义断言
 * @modified By：
 * @version: $
 */
@Component
public class CustomizetRoutePredicateFactory extends AbstractRoutePredicateFactory<CustomizetRoutePredicateFactory.Config> {


    public CustomizetRoutePredicateFactory() {
        super(CustomizetRoutePredicateFactory.Config.class);
    }

    public CustomizetRoutePredicateFactory(Class<Config> configClass) {
        super(configClass);
    }

    @Override
    public Predicate<ServerWebExchange> apply(Consumer<Config> consumer) {
        return super.apply(consumer);
    }

    @Override
    public AsyncPredicate<ServerWebExchange> applyAsync(Consumer<Config> consumer) {
        return super.applyAsync(consumer);
    }

    @Override
    public void beforeApply(Config config) {
        super.beforeApply(config);
    }

    @Override
    public Predicate<ServerWebExchange> apply(CustomizetRoutePredicateFactory.Config config) {
        return new Predicate<ServerWebExchange>() {
            @Override
            public boolean test(ServerWebExchange serverWebExchange) {
                //1 接收前台传入的name参数
                String name = serverWebExchange.getRequest().getQueryParams().getFirst("name");
                //2 先判断是否为空
                if (StringUtils.isNotEmpty(name)) {
                    //3 如果不为空,再进行路由逻辑判断,与断言中配置进行对比判断
                    if (name.equals(config.getName())) {
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            }
        };
    }

    /**
     * 读取配置文件的中参数值 给他赋值到配置类中的属性上
     */
    @Override
    public List<String> shortcutFieldOrder() {
        //这个位置的顺序必须跟配置文件中的值的顺序对应
        return Arrays.asList("name");
    }

    @Override
    public AsyncPredicate<ServerWebExchange> applyAsync(Config config) {
        return super.applyAsync(config);
    }

    @Override
    public String name() {
        return super.name();
    }

    @Override
    public ShortcutType shortcutType() {
        return super.shortcutType();
    }


    @Override
    public String shortcutFieldPrefix() {
        return super.shortcutFieldPrefix();
    }

    //用于接收配置文件中断言的信息
    @Validated
    public static class Config {
        /**
         * 断言校验属性
         */
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
